package com.lora.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class StringUtil {

    public static String filepath(){
        File upload=new File(System.getProperty("user.dir"),"");
        if(!upload.exists()){
            upload.mkdirs();
        }
        String filePath = upload.getAbsolutePath();
        return filePath+File.separator;
    }
    /**
     * 根据长度生产随机数,字符和数字
     * @param length
     * @return
     */
    public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 16进制转2进制
     * @param hex
     * @return
     */
    public static String hexStringToByte(String hex) {
        int index = Integer.parseInt(hex, 16);
        String bat=Integer.toBinaryString(index);
        for (int i = bat.length(); i <8 ; i++) {
            bat="0"+bat;
        }
        return bat;
    }

    public static String fullzore(String bat) {
        for (int i = bat.length(); i <8 ; i++) {
            bat="0"+bat;
        }
        return bat;
    }

    public static String fullzore_128(String bat) {
        for (int i = bat.length(); i <128 ; i++) {
            bat=bat+"0";
        }
        return bat;
    }

    public static String intToHex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            s = s.append(b[n%16]);
            n = n/16;
        }
        a = s.reverse().toString();
        return a;
    }
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * 16进制转10进制
     * @param hex
     * @return
     */
    public static int hexStringToInt(String hex) {
        return Integer.parseInt(hex, 16);
    }

    public static String dateToStr(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String getMonth(){
        Date date=new Date();
        int year = date.getYear() + 1900;
        Integer month=date.getMonth()+1;
        String strmonth=month<10?"0"+month:month+"";
        return year+"-"+strmonth;
    }
}
